//
//  BTEProductView.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEProductView: UIView {
    
    var load:(()->())?
    
    var clickItem:((Int)->())?
    
    var clickBy:(()->())?
    
    var clickPro:(()->())?

    var mod: BTEProductNetModel? {
        didSet {
            guard let m = mod else {
                return 
            }
            list.reloadData()
            
            loginBut.text = m.nonchurchgoing?.allergin
            
            if let m1 = m.earworms?.aftermath,m1.count > 0 {
                privicyView.isHidden = false
            }else {
                privicyView.isHidden = true
            }
        }
    }
    
    var refreshfunc: UIRefreshControl!
    
    let loginButton = UIImageView.init(image: UIImage.init(named: "Rectangle xxxxxx95"))
    let bo = UIView()
    let loginBut = UILabel.init()
    
    let privicyView = PrivacyAgreementView()
    
    let list = BTEBaseTableView.init(frame: .zero, style: .plain)
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        addSubview(bo)
        bo.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(18)
            make.bottom.equalToSuperview().offset(-34)
        }
        
        privicyView.configure(withText: BTELanguageManager.shared.localizedString(forKey: "I have read and agree"), linkText: BTELanguageManager.shared.localizedString(forKey: "<Loan Agreement>"), fontSize: 14)
        bo.addSubview(privicyView)
        privicyView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        bo.addSubview(loginButton)
        loginButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(56)
            make.bottom.equalToSuperview()
            make.top.equalTo(privicyView.snp.bottom).offset(12)
        }
        
       
        loginBut.textColor = .colorWithHex("#ffffff")
        loginBut.font = .systemFont(ofSize: 16,weight: .bold)
        loginButton.addSubview(loginBut)
        loginBut.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        loginButton.isUserInteractionEnabled  = true
        let tab = UITapGestureRecognizer.init(target: self, action: #selector(clickbutton))
        loginButton.addGestureRecognizer(tab)
        
        privicyView.didTapPrivacyLink = {[weak self] in
            self?.clickPro?()
        }
    
        list.showsHorizontalScrollIndicator = false
        list.backgroundColor = .clear
        addSubview(list)
        list.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(loginButton.snp.top).offset(-10)
        }
        list.delegate = self
        list.dataSource = self
        list.separatorStyle = .none
        
        list.register(BTEProductTopTableViewCell.self, forCellReuseIdentifier: BTEProductTopTableViewCell.indetification)
        list.register(BTEProductlistTableViewCell.self, forCellReuseIdentifier: BTEProductlistTableViewCell.indetification)
        
        refreshfunc = UIRefreshControl()
        refreshfunc.addTarget(self, action: #selector(loadInfoaction), for: .valueChanged)
        list.refreshControl = refreshfunc
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    @objc func loadInfoaction(){
        load?()
    }
    
    @objc func clickbutton(){
        clickBy?()
    }
    
    @objc func clickprobutton(){
        clickPro?()
    }


}



extension BTEProductView: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            guard let ce = tableView.dequeueReusableCell(withIdentifier: BTEProductTopTableViewCell.indetification, for: indexPath) as? BTEProductTopTableViewCell else {
                return UITableViewCell()
            }
            ce.mod = mod
            return ce
        } else if indexPath.row == 1 {
            guard let ce = tableView.dequeueReusableCell(withIdentifier: BTEProductlistTableViewCell.indetification, for: indexPath) as? BTEProductlistTableViewCell else {
                return UITableViewCell()
            }
            ce.configure(with: mod?.ebon ?? [])
            
            ce.clickItem = {[weak self] idx in
                self?.clickItem?(idx)
            }
            return ce
        }
        return UITableViewCell()
    }
    

}



